﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Simple.Common.Core.Utils
{
    public class TimeUtil
    {
        /// <summary>
        /// 获取时间戳(整数秒)
        /// </summary>
        public static string GetTimeSpan()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>
        /// 获取时间戳(整数秒)
        /// </summary>
        /// <param name="dateTimeString"></param>
        /// <returns></returns>
        public static string GetTimeSpan(string dateTimeString)
        {
            DateTime datetime;
            if (DateTime.TryParse(dateTimeString, out datetime))
            {
                TimeSpan ts = datetime.ConvertTimeToUtc() - new DateTime(1970, 1, 1, 0, 0, 0, 0);

                return Convert.ToInt64(ts.TotalSeconds).ToString();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取年月日
        /// <para>yyyyMMdd(20180101)</para>
        /// </summary>
        public static string GetYMD
        {
            get { return DateTime.Now.ToString("yyyyMMdd"); }
        }
    }
}
